//
// Created by hjie on 23-6-22.
//

#pragma once

#include <string>
#include <memory>
#include <functional>
#include <map>
#include <rapidjson/document.h>
#include <vector>
#include "base_logic_module.h"

enum POLICY
{
    RESPONSE  = 0,   // default
    BROADCAST = 1,   // broadcast
    TARGET    = 2,   // target user id
};

class RoomReferenceData;

using ProtocolFunc = std::function<void(RoomReferenceData *, std::shared_ptr<BaseProtocol>&)>;

class MessageRouter
{
public:
    static MessageRouter * Instance()
    {
        if (s_m_router == nullptr)
        {
            s_m_router = new MessageRouter();
            BaseLogicModule::RegisterLogicModule();
        }
        return s_m_router;
    }
public:
    MessageRouter();
    ~MessageRouter();
    void AddLogicModule(BaseLogicModule * module);
public:
    std::map<std::string, BaseLogicModule*> m_module_map;
    std::map<std::string, std::function<void(RoomReferenceData * data,rapidjson::Document & , rapidjson::Document &)>> m_message_func_map;
    std::map<std::string, ProtocolFunc> m_message_protocol_func_map;
    static MessageRouter * s_m_router;
};


#define REGISTER_MSG_TYPE(msg_type, type, func, data) \
    do          \
    {           \
        MessageRouter::Instance()->m_message_protocol_func_map[msg_type] = std::bind(&type::func, data, std::placeholders::_1, std::placeholders::_2); \
                \
                \
    }while(0);





